<?php
require_once COMMON_PATH . 'Common/constant.php';

//定义短信模板标识
//客栈已审核开通发送短信密码
define('SMS_TEMPLETE_NOT_REGISTER', '20296');
//客栈拒绝短信标识
define('SMS_TEMPLETE_APPLY_REFUSE', '75332');
//经销商短信标识
define('SMS_TEMPLETE_DEALER_PASS', '148258');
define('SMS_TEMPLETE_DEALER_REFUSE', '148259');
define('SMS_TEMPLETE_DEALER_HOTEL_PASS', '148260');

/**
 * 返回管理员ID
 * @return type
 */
function getAdminId() {
    return isset($_SESSION['adminId']) ? $_SESSION['adminId'] : 0;
}

/**
 * 
 * @return type返回管理员名称
 */
function getAdminName() {
    return isset($_SESSION['adminName']) ? $_SESSION['adminName'] : '';
}

/**
 * 验证验证码
 */
function checkVcode($vcode, $id = '') {
    if (!$vcode) {
        return false;
    }
    $verify = new \Think\Verify();
    return $verify->check($vcode, $id);
}

/**
 * 
 * @param type $httpstatus
 * @param type $status
 * @param type $data
 * @param type $jumpUrl
 */
function returnJson($httpstatus = 200, $status = '', $data = '') {
    static $http = array(
        100 => "HTTP/1.1 100 Continue",
        101 => "HTTP/1.1 101 Switching Protocols",
        200 => "HTTP/1.1 200 OK",
        201 => "HTTP/1.1 201 Created",
        202 => "HTTP/1.1 202 Accepted",
        203 => "HTTP/1.1 203 Non-Authoritative Information",
        204 => "HTTP/1.1 204 No Content",
        205 => "HTTP/1.1 205 Reset Content",
        206 => "HTTP/1.1 206 Partial Content",
        300 => "HTTP/1.1 300 Multiple Choices",
        301 => "HTTP/1.1 301 Moved Permanently",
        302 => "HTTP/1.1 302 Found",
        303 => "HTTP/1.1 303 See Other",
        304 => "HTTP/1.1 304 Not Modified",
        305 => "HTTP/1.1 305 Use Proxy",
        307 => "HTTP/1.1 307 Temporary Redirect",
        400 => "HTTP/1.1 400 Bad Request",
        401 => "HTTP/1.1 401 Unauthorized token error",
        402 => "HTTP/1.1 402 Payment Required",
        403 => "HTTP/1.1 403 Forbidden",
        404 => "HTTP/1.1 404 Not Found",
        405 => "HTTP/1.1 405 Method Not Allowed",
        406 => "HTTP/1.1 406 Not Acceptable",
        407 => "HTTP/1.1 407 Proxy Authentication Required",
        408 => "HTTP/1.1 408 Request Time-out",
        409 => "HTTP/1.1 409 Conflict",
        410 => "HTTP/1.1 410 Gone",
        411 => "HTTP/1.1 411 Length Required",
        412 => "HTTP/1.1 412 Precondition Failed",
        413 => "HTTP/1.1 413 Request Entity Too Large",
        414 => "HTTP/1.1 414 Request-URI Too Large",
        415 => "HTTP/1.1 415 Unsupported Media Type",
        416 => "HTTP/1.1 416 Requested range not satisfiable",
        417 => "HTTP/1.1 417 Expectation Failed",
        500 => "HTTP/1.1 500 Internal Server Error",
        501 => "HTTP/1.1 501 Not Implemented",
        502 => "HTTP/1.1 502 Bad Gateway",
        503 => "HTTP/1.1 503 Service Unavailable",
        504 => "HTTP/1.1 504 Gateway Time-out",
    );
    $result = array();
    $result['data'] = $data;
    $result['info'] = $data;
    $result['status'] = intval($status);

    header($http[$httpstatus]);
    header('Content-Type:application/json; charset=utf-8');
    echo json_encode($result, JSON_UNESCAPED_UNICODE);
    exit;
}

/**
 * 分页操作
 * @param integer $count
 * @return \Think\Page
 */
function classPage($count, $pageSize= 20, $link = '') {
    $Page = new Think\Page($count, $pageSize);
    if (!empty($link)) {
        $Page->setConfig('link', $link);
    }
    return $Page;
}

/**
 * 输出条件查找
 * @param type $a 方法名称
 * @param type $c 控制器名称
 * @param type $m 分组名称
 */
function echoSearch($a = ACTION_NAME, $c = CONTROLLER_NAME, $m = MODULE_NAME) {
    $p = isset($_GET['p']) ? $_GET['p'] : 1;
    echo "<input type=\"hidden\"  value=" . $p . " name='p'id='p'/> " .
    "<input type=\"hidden\"  value=" . $m . " name='m' id='m' />" .
    "<input type='hidden' value='$c' name='c' id='c'>" .
    "<input type='hidden' value='$a' name='a' id='a'>";
}

/**
 * 输出条件查找
 * @param type $a 方法名称
 * @param type $c 控制器名称
 * @param type $m 分组名称
 */
function echoSearchCharity($a = ACTION_NAME, $c = CONTROLLER_NAME, $m = MODULE_NAME) {
    $p = isset($_GET['p']) ? $_GET['p'] : 1;
    echo "<input type=\"hidden\"  value=" . $p . " name='p'id='p'/> " .
    "<input type=\"hidden\"  value=" . $m . " name='m' id='m' />" .
    "<input type='hidden' value='$c' name='c' id='c'>" .
    "<input type='hidden' value='$a' name='a' id='a'>" .
    "<input type='hidden' value='1' name='z' id='z'>" ;
}

/**
 * 返回状态数据
 * @param integer $status  申请状态值
 * @return string
 */
function getApplyStatus($status = -1) {
    $statusArr = array(0 => '未处理', 1 => '已处理', 2 => '已拒绝');
    if ($status == -1) {
        return $statusArr;
    }
    switch ($status) {
        case 0:
            return "<span style=\"\">" . $statusArr[0] . "</span>";
        case 1:
            return "<span style=\"color:green;\">" . $statusArr[1] . "</span>";
        case 2:
            return "<span style=\"color:red;\">" . $statusArr[2] . "</span>";
        default :
            return "<span>未知</span>";
    }
}

/**
 * 
 * 返回客栈状态
 * @param integer $status 客栈状态值
 * @return string
 */
function getHotelStatus($status = 0) {
    $statusArr = array(0 => '禁用', 1 => '开通', 2 => '试用', 3 => '测试', 4 => '免费');
    switch ($status) {
        case 0:
            return "<span style=\"\">" . $statusArr[0] . "</span>";
        case 1:
            return "<span style=\"color:green;\">" . $statusArr[1] . "</span>";
        case 2:
            return "<span style=\"color:blue;\">" . $statusArr[2] . "</span>";
        case 3:
            return "<span style=\"color:blue;\">" . $statusArr[3] . "</span>";
        case 4:
            return "<span style=\"color:blue;\">" . $statusArr[4] . "</span>";
        default :
            return "<span>未知</span>";
    }
}

/**
 *  生成随机密码(五位数字+一位字母)
 * @return string
 */
function getRandomPassword() {
    $pattern = '1234567890';
    $length = 5;
    for ($i = 0; $i < $length; $i++) {
        $token .= $pattern[rand(0, 9)];
    }
    $letter = 'abcdefghijklmnopqrstuvwxyz';
    $token = $token . $letter[rand(0, 25)];
    return $token;
}

function getArrayValueAsIndex($dataArray, $keyName, $multi = false) {
    $dataArray = array_values($dataArray);
    if(empty($dataArray) || !isset($dataArray[0][$keyName])) {
        return array();
    }
    $indexArray = array();
    if($multi){
        foreach ($dataArray as $dataItem) {
            if(empty($indexArray[$dataItem[$keyName]])) {
                $indexArray[$dataItem[$keyName]] = array();
            }
            array_push($indexArray[$dataItem[$keyName]], $dataItem);
        }
    }else {
        foreach ($dataArray as $dataItem) {
            if(!isset($indexArray[$dataItem[$keyName]])) {
                $indexArray[$dataItem[$keyName]] = $dataItem;
            }
        }
    }
    unset($dataArray);
    unset($dataItem);
    return $indexArray;
}

/**
 * 对数据处理并产生分页结果集
 * @modify AmosZhong 2015-09-27
 * @param  array $dataList  数据
 * @param  integer $pageIndex 页码
 * @param  integer $pageSize 页数
 * @return array
 */
function createPageInfo($dataList, $pageIndex, $pageSize) {
    $dataTotal = count($dataList);
    $offset = ($pageIndex - 1) * $pageSize;
    $endOffset = $offset + $pageSize;
    $dataResult = array();
    for ($i = $offset; $i < $endOffset && $i < $dataTotal; $i++) {
        $dataResult[] = $dataList[$i];
    }
    $pageTotal = ceil($dataTotal / $pageSize);
    return array('data' => $dataResult, 'current' => $pageIndex, 'size' => $pageSize, 'total' => $dataTotal, 'pageTotal' => $pageTotal);
}

/**
 * 生成分页信息
 * @modify trainyao 2016-01-08 创建
 * @modify wenhui 2017-01-03 修改客栈信息
 * @param  array   $allHotelinfo 客栈信息
 * @param  integer   $pageIndex 页码
 * @param  integer   $pageSize  每页大小
 * @return array              array('hotelInfo' => (客栈信息,不为空), 'page' => (分页信息:页码,总页数,每页长度,总条目数))
 */
function generatePageInfo($allHotelinfo, $pageIndex, $pageSize) {
    if (empty($pageIndex) || empty($pageSize)) {
        return array('hotelInfo' => $allHotelinfo);
    }

    // 处理分页
    $pageInfo = array();
    $allHotelinfo = createPageInfo($allHotelinfo, $pageIndex, $pageSize);
    $pageInfo = array(
        'current' => $allHotelinfo['current'],
        'size' => $allHotelinfo['size'],
        'total' => $allHotelinfo['total'],
        'pageTotal' => $allHotelinfo['pageTotal'],
    );
    $allHotelinfo = $allHotelinfo['data'];

    return array('hotelInfo' => $allHotelinfo, 'page' => $pageInfo);
}
